package com.example.shop_2208.search

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.TextView
import androidx.appcompat.widget.SearchView
import androidx.lifecycle.lifecycleScope

import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.shop_2208.R
import com.example.shop_2208.adapter.GoodsAdapter
import com.example.shop_2208.adapter.GoodsMultiAdapter
import com.example.shop_2208.adapter.HistoryAdapter
import com.example.shop_2208.databinding.ActivitySearchBinding
import com.example.shop_2208.db.MyDbUtils
import com.example.shop_2208.entity.SearchEntity
import com.example.shop_2208.mvvm.BaseActivity
import kotlinx.coroutines.launch

@Route(path = "/app/search")
class SearchActivity : BaseActivity<ActivitySearchBinding,SearchViewModel>() {
    lateinit var goodsAdapter: GoodsMultiAdapter
    lateinit var historyAdapter: HistoryAdapter
    override fun initData() {
        viewModel.searchSuccess.observe(this){
            if(it.isNotEmpty()){
                goodsAdapter.data.clear()
                it.forEachIndexed { index, goodsEntity ->
                    goodsEntity.type = index%2
                    goodsAdapter.data.add(goodsEntity)
                }
                goodsAdapter.notifyDataSetChanged()
            }else{
                ToastUtils.showLong("未搜索到符合条件的数据，换个关键词试试吧")
                goodsAdapter.data.clear()
                goodsAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun initView() {
        goodsAdapter = GoodsMultiAdapter()
        binding.rvSv.adapter = goodsAdapter
        binding.rvSv.layoutManager = GridLayoutManager(this,2)

        //执行搜索功能
        binding.sv.setOnQueryTextListener(object: SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                //点击回车时触发
                if(query!=null){
                    viewModel.search(query)
                    //保存搜索记录
                    MyDbUtils.getDb().getSearchDao().insertSearch(SearchEntity(query,0L))
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                //文本内容发生变化时触发
                return true
            }
        })

        //初始化搜索记录
       /* historyAdapter = HistoryAdapter()
        binding.rvSearch.adapter = historyAdapter
        binding.rvSearch.layoutManager = GridLayoutManager(this,3)
        historyAdapter.data.clear()
        historyAdapter.data.addAll(MyDbUtils.getDb().getSearchDao().querySearch())
        historyAdapter.notifyDataSetChanged()*/

        val data = MyDbUtils.getDb().getSearchDao().querySearch()
        if(data!=null){
            binding.cg.removeAllViews()
            data.forEach {
                val key = it.name
                val view = LayoutInflater.from(this).inflate(R.layout.item_chip,null)
                view.findViewById<TextView>(R.id.key).text = it.name//给id为chip赋值
                binding.cg.addView(view)
                //点击item,实现查询
                view.setOnClickListener {
                    binding.sv.setQuery(key,true)
                }
            }
        }
    }

    override fun getViewModel(): Class<SearchViewModel> = SearchViewModel::class.java

    override fun getLayout(): Int = R.layout.activity_search

}